package net.kts.util.url;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.StringReader;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.ProtocolException;
import java.net.URL;
import java.net.URLEncoder;

import org.simpleframework.xml.Serializer;
import org.simpleframework.xml.core.Persister;

import net.kts.attraction.search.model.YahooAddress;


public class RequestURL {
	private static String yahooApiKey = "6xWu8G_V34HEhDKaVP5QCPn5U5Y6ZHbLLJr1AWcRDOuHtCSpoZRjYqtoJ58SK1pQPbR_ZIAfOkCIO5V_Kh.e7Ns.thjnWDo-";
	private static String results = "10";
	private static String yahoo = "http://kr.open.gugi.yahoo.com/service/poi.php?appid="+yahooApiKey+"&results="+ results+"&q=";
	
	public static YahooAddress requestYahooAddress(String searchText) {
		
		String result = null;
		try {
			result = requestUrlToString(yahoo, URLEncoder.encode(searchText, "UTF-8"));
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
					
		return parsingToYahooAddress(result);
	}
	
	private static YahooAddress parsingToYahooAddress(String xml) {
		Serializer serializer = new Persister();
		Reader reader = new StringReader(xml);
        YahooAddress ya = null;
        try {
			
			   ya = serializer.read(YahooAddress.class, reader, false);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
        
        return ya;
		
	}
	
	public static String requestUrlToString(String url, String parameter) {
		String result = null;
		
		HttpURLConnection connection = null;
//		OutputStreamWriter wr = null;
		BufferedReader rd  = null;
		StringBuilder sb = null;
		String line = null;

		URL serverAddress = null;

		try {
			serverAddress = new URL(url + parameter);
			//set up out communications stuff
			connection = null;

			//Set up the initial connection
			connection = (HttpURLConnection)serverAddress.openConnection();
			connection.setRequestMethod("POST");
			connection.setDoOutput(true);
			connection.setReadTimeout(10000);

			connection.connect();

			//read the result from the server
			rd  = new BufferedReader(new InputStreamReader(connection.getInputStream()));
			sb = new StringBuilder();

			while ((line = rd.readLine()) != null)
			{
				sb.append(line + "\r\n");
			}
			result = sb.toString();
//			System.out.println(sb.toString());
			
			

		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (ProtocolException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		finally
		{
			//close the connection, set all objects to null
			connection.disconnect();
			rd = null;
			sb = null;
//			wr = null;
			connection = null;
		}
		
		
		return result;
	}

}
